﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace LyncServerExtension.Plugin.JSON
{
    /// <summary>
    /// Class which implements the JSON result builder as a loadable plugin.
    /// </summary>
    public sealed class JSONResultBuilder : LyncResultBuilderPlugin 
    {
        private JSONSerializer serializer = new JSONSerializer();

        /// <summary>
        /// Gets the plugin id.
        /// </summary>
        public override string Id
        {
            get { return "JSON"; }
        }

        /// <summary>
        /// Gets the plugin version.
        /// </summary>
        public override string Version
        {
            get { return "1.1"; }
        }

        /// <summary>
        /// Sets the authorized owners.
        /// </summary>
        /// <param name="users">The users.</param>
        /// <returns>The result.</returns>
        public override ILyncResult SetAuthorizedOwners(IEnumerable<LyncUser> users)
        {
            return serializer.Serialize(
                new
                {
                    Type = "LyncResult",
                    Users = users
                });
        }

        /// <summary>
        /// Sets the authorized delegates.
        /// </summary>
        /// <param name="ownerSip">The owner sip.</param>
        /// <param name="users">The users.</param>
        /// <returns>The result.</returns>
        public override ILyncResult SetAuthorizedDelegates(string ownerSip, IEnumerable<LyncUser> users)
        {
            return serializer.Serialize(
                new
                {
                    Type = "LyncResult",
                    OwnerSip = ownerSip,
                    Users = users
                });
        }

        /// <summary>
        /// Sets the delegates.
        /// </summary>
        /// <param name="ownerSip">The owner sip.</param>
        /// <param name="users">The users.</param>
        /// <returns>The result.</returns>
        public override ILyncResult SetDelegates(string ownerSip, IEnumerable<LyncUser> users)
        {
            return serializer.Serialize(
                new
                {
                    Type = "LyncResult",
                    OwnerSip = ownerSip,
                    Users = users
                });
        }

        /// <summary>
        /// Sets the call forwarding delegation.
        /// </summary>
        /// <param name="param">The param.</param>
        /// <param name="state">The state.</param>
        /// <returns>The result.</returns>
        public override ILyncResult SetCallForwardingDelegation(LyncCallForwardingParameter param, string state)
        {
            return serializer.Serialize(
                new
                {
                    Type = "LyncResult",
                    Enabled = param.IsEnabled,
                    OwnerSip = param.OwnerSip,
                    DelegateSip = param.DelegateSip
                });
        }

        /// <summary>
        /// Errors the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>The result.</returns>
        public override ILyncResult Error(string message)
        {
            return serializer.Serialize(
                new
                {
                    Type = "LyncError",
                    Message = message
                });
        }
    }
}
